﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyShopThoiTrang_DAO
{
    public class SanPhamDAO
    {
        public static List<SanPhamDTO> LayDS()
        {
            return null;
        }

        public static bool SanPham_Them(SanPhamDTO sp)
        {
            bool kq = true;
            DataProvider kn = DataProvider.Instance();
            try
            {
                string sql = "Insert into SanPham(MaNhaCungCap, MaLoaiSanPham,SanPham_Ten, SanPham_NhanHieu, SanPham_XuatXu, SanPham_GiaKhiMua,";
                sql += "SanPham_GiaKhiBan, SanPham_GiaUuDai, SanPham_TrangThai, SanPham_KichCo, SanPham_SoLuong, SanPham_HinhAnh, SanPham_MaHang";
                sql += " values(@MaNhaCungCap, @MaLoaiSanPham, @SanPham_Ten, @SanPham_NhanHieu, @SanPham_XuatXu, @SanPham_GiaKhiMua,";
                sql += "@SanPham_GiaKhiBan, @SanPham_GiaUuDai, @SanPham_TrangThai, @SanPham_KichCo, @SanPham_SoLuong, @SanPham_HinhAnh, @SanPham_MaHang";
                DataProvider.OpenConnection();
                SqlCommand com = kn.Connection.CreateCommand();
                com.CommandText = sql;
                SqlParameter par = new SqlParameter("@MaNhaCungCap", SqlDbType.Int);
                par.Value = sp.NhaCungCap;
                com.Parameters.Add(par);

                par = new SqlParameter("@MaLoaiSanPham", SqlDbType.Int);
                par.Value = sp.LoaiSanPham;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_Ten", SqlDbType.NVarChar);
                par.Value = sp.SanPham_Ten;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_NhanHieu", SqlDbType.NVarChar);
                par.Value = sp.SanPham_NhanHieu;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_XuatXu", SqlDbType.NVarChar);
                par.Value = sp.SanPham_XuatXu;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_GiaKhiMua", SqlDbType.Float);
                par.Value = sp.SanPham_GiaKhiMua;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_GiaKhiBan", SqlDbType.Float);
                par.Value = sp.SanPham_GiaKhiBan;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_GiaUuDai", SqlDbType.Float);
                par.Value = sp.SanPham_GiaUuDai;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_TrangThai", SqlDbType.VarChar);
                par.Value = sp.SanPham_TrangThai;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_KichCo", SqlDbType.VarChar);
                par.Value = sp.SanPham_KichCo;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_SoLuong", SqlDbType.Int);
                par.Value = sp.SanPham_SoLuong;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_HinhAnh", SqlDbType.VarChar);
                par.Value = sp.SanPham_HinhAnh;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_MaHang", SqlDbType.VarChar);
                par.Value = sp.SanPham_MaHang;
                com.Parameters.Add(par);

                com.ExecuteNonQuery();
            }
            catch
            {
                kq = false;
            }
            finally
            {
                DataProvider.CloseConnection();
            }
            return kq;
        }

        public static bool SanPham_CapNhat(SanPhamDTO sp)
        {
            bool kq = true;
            DataProvider kn = DataProvider.Instance();
            try
            {
                string sql = "update SanPham set MaNhaCungCap = @MaNhaCungCap, MaLoaiSanPham = @MaLoaiSanPham, SanPham_Ten = @SanPham_Ten, SanPham_NhanHieu = @SanPham_NhanHieu, ";
                sql += "SanPham_GiaKhiBan = @SanPham_GiaKhiBan, SanPham_GiaUuDai = @SanPham_GiaUuDai, SanPham_TrangThai = @SanPham_TrangThai, SanPham_KichCo = @SanPham_KichCo, ";
                sql += "SanPham_SoLuong = @SanPham_SoLuong, SanPham_HinhAnh = @SanPham_HinhAnh, SanPham_MaHang = @SanPham_MaHang, ";
                sql += "SanPham_XuatXu = @SanPham_XuatXu, SanPham_GiaKhiMua = @SanPham_GiaKhiMua where MaSanPham = @MaSanPham";
                DataProvider.OpenConnection();
                SqlCommand com = kn.Connection.CreateCommand();
                SqlParameter par = new SqlParameter("@MaNhaCungCap", SqlDbType.Int);
                par.Value = sp.NhaCungCap;
                com.Parameters.Add(par);

                par = new SqlParameter("@MaLoaiSanPham", SqlDbType.Int);
                par.Value = sp.LoaiSanPham;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_Ten", SqlDbType.NVarChar);
                par.Value = sp.SanPham_Ten;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_NhanHieu", SqlDbType.NVarChar);
                par.Value = sp.SanPham_NhanHieu;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_XuatXu", SqlDbType.NVarChar);
                par.Value = sp.SanPham_XuatXu;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_GiaKhiMua", SqlDbType.Float);
                par.Value = sp.SanPham_GiaKhiMua;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_GiaKhiBan", SqlDbType.Float);
                par.Value = sp.SanPham_GiaKhiBan;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_GiaUuDai", SqlDbType.Float);
                par.Value = sp.SanPham_GiaUuDai;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_TrangThai", SqlDbType.VarChar);
                par.Value = sp.SanPham_TrangThai;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_KichCo", SqlDbType.VarChar);
                par.Value = sp.SanPham_KichCo;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_SoLuong", SqlDbType.Int);
                par.Value = sp.SanPham_SoLuong;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_HinhAnh", SqlDbType.VarChar);
                par.Value = sp.SanPham_HinhAnh;
                com.Parameters.Add(par);

                par = new SqlParameter("@SanPham_MaHang", SqlDbType.VarChar);
                par.Value = sp.SanPham_MaHang;
                com.Parameters.Add(par);

                par = new SqlParameter("@MaSanPham", SqlDbType.Int);
                par.Value = sp.MaSanPham;
                com.Parameters.Add(par);

                com.ExecuteNonQuery();
            }
            catch
            {
                kq = false;
            }
            finally
            {
               DataProvider.CloseConnection();
            }
            return kq;
        }

        public static bool SanPham_Xoa(SanPhamDTO sp)
        {
            bool kq = true;
            DataProvider kn = DataProvider.Instance();
            try
            {
                string sql = "update SanPham set SanPham_TrangThai = @SanPham_TrangThai where MaSanPham = @MaSanPham";
                DataProvider.OpenConnection();
                SqlCommand com = kn.Connection.CreateCommand();
                com.CommandText = sql;
                SqlParameter par = new SqlParameter("@SanPham_TrangThai", SqlDbType.VarChar);
                par.Value = sp.SanPham_TrangThai;
                com.Parameters.Add(par);

                par = new SqlParameter("@MaSanPham", SqlDbType.Int);
                par.Value = sp.MaSanPham;
                com.Parameters.Add(par);
                com.ExecuteNonQuery();
            }
            catch
            {
                kq = false;
            }
            finally
            {
                DataProvider.CloseConnection();
            }
            return kq;
        }
    }
}
